# Multi-stage build for User Service
FROM golang:1.24-alpine AS builder

# 设置工作目录
WORKDIR /app

# 安装必要的工具
RUN apk add --no-cache git ca-certificates tzdata

# 复制 go mod 文件
COPY go.mod go.sum ./
RUN go mod download

# 复制所有源代码
COPY . .

# 构建应用
RUN CGO_ENABLED=0 GOOS=linux go build \
    -ldflags='-w -s -extldflags "-static"' \
    -a -installsuffix cgo \
    -o userservice \
    services/userservice/cmd/main.go

# 运行阶段
FROM scratch

# 从 builder 阶段复制时区信息
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
# 从 builder 阶段复制 CA 证书
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# 复制编译好的应用
COPY --from=builder /app/userservice /userservice
# 复制配置文件
COPY --from=builder /app/services/userservice/configs /configs

# 设置非 root 用户
USER 1000

# 暴露端口
EXPOSE 8000 9000

# 运行应用
ENTRYPOINT ["/userservice"]
